﻿using System;
using System.Windows.Forms;

namespace Administrator
{
    public partial class ucPulseMetersCount : UserControl
    {
        private string m_PreviosValue;
        public ucPulseMetersCount()
        {
            InitializeComponent();
        }
        
        public void FillFields(Int32 metersCount)
        {

            m_PreviosValue = cbPulseMetersCount.Text = metersCount.ToString();
        }

        public bool GetMetersCount(ref Int32 MetersCount)
        {
            if (cbPulseMetersCount.Text == "")
            {
                MessageBox.Show(cConst.ERR_UNK_DEVICENUM_OFFSET, cConst.ERROR_CAPTION,
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            MetersCount = Convert.ToInt32(cbPulseMetersCount.Text);
            
            return true;
        }

        private void cbPulseMetersCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Type t = 
            //if (((e.KeyChar == (char)Keys.Delete) || (e.KeyChar == (char)Keys.Back)) &&
            //    (((ComboBox)sender).Text.Length < 2))
            //    e.Handled = true;
            //else  
            if (!(Char.IsDigit(e.KeyChar)) && (e.KeyChar != (char)Keys.Delete) && (e.KeyChar != (char)Keys.Back))
                e.Handled = true;
            else if (((ComboBox)sender).Text.Length == 1 && ((ComboBox)sender).Text == "0" && Char.IsDigit(e.KeyChar))
            {
                ((ComboBox)sender).Text = "";
                base.OnKeyPress(e);
              
            }
            //else if (e.KeyChar != (char)Keys.Back &&
            //    e.KeyChar != (char)Keys.Delete &&
            //    Convert.ToInt32(((ComboBox)sender).Text + e.KeyChar) > 16)
            //    e.Handled = true;
            else
                base.OnKeyPress(e);
            

            
            
        }

        private void cbPulseMetersCount_TextChanged(object sender, EventArgs e)
        {
            //Type t = sender.GetType();
            //if (((ComboBox)sender).Text.Length == 0)
            //    ((ComboBox)sender).Text = m_PreviosValue;
            //else
            if (((ComboBox)sender).Text.Length > 0 && Convert.ToInt32(((ComboBox)sender).Text) > 16)
            {
                ((ComboBox)sender).Text = m_PreviosValue;
                if (((ComboBox)sender).SelectionStart == 0)
                    ((ComboBox)sender).SelectionStart = ((ComboBox)sender).Text.Length;
            }
            else
                if (((ComboBox)sender).Text.Length > 2)
                {
                    ((ComboBox)sender).Text = m_PreviosValue;
                   // ((ComboBox)sender).SelectionStart = ((ComboBox)sender).Text.Length;
                }
            else
                m_PreviosValue = ((ComboBox)sender).Text;
           
            //    else
            //        m_PreviosValue = ((ComboBox)sender).Text;
        }
    }
}
